import {getToken} from "./auth.js"

export const config = {
    baseUrl: "http://localhost:9002/"
}

const request = async function (url, parms, method) {

    let baseURl = config.baseUrl;

    try {
        const pages = getCurrentPages() // 应用页面地址沾
        const token = getToken()
        const pageTargetPath = pages[pages.length - 1].route // 当前的地址path
        if (!token && pageTargetPath !== 'pages/login/login') { //当前页面不是登录页面跳转
           logError()
        }

		uni.showLoading({
			title:"加载中！",
			mask:true
		})
        const result = await uni.request({
            url: baseURl + url,
            method,
            data: parms || {},
			header:{
				'Authorization':token
			}
			
        })
		uni.hideLoading()	
		if (result[1].data.code == 401) {
			logError()
			removeToken()
		}
	
        return result[1].data;


    } catch (e) {
       console.log(e)
    }
}

export default request;

function logError(){
	uni.showToast({
		icon:"error",
		title:"身份信息过期！",
		duration:2000
	})
	setTimeout(()=>{
		uni.redirectTo({
		    url: '/pages/login/login'
		})
	},2000)
}